package com.sy.springboottest.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sy.springboottest.dao.BaseAreaReposity;
import com.sy.springboottest.dao.RefReposity;
import com.sy.springboottest.pojo.BaseArea;
import com.sy.springboottest.pojo.Demo;
import com.sy.springboottest.pojo.Page;
import com.sy.springboottest.pojo.RefDemo;

@Controller
@RequestMapping("/refresh/")
public class RefreshController {
	@Autowired
	private BaseAreaReposity baseAreaReposity;
	@Autowired
	private RefReposity refReposity;
	
	
	@RequestMapping("j/listprovince")
	public String listprovince(Model model) {
		List<BaseArea> provinces = baseAreaReposity.findProvices();
		model.addAttribute("listprovinces", provinces);
		return "jsp/indexprovince";
	}
	
	
	@PostMapping("t/listcity")
	public String listcity(Model model,RefDemo ref) {
		List<BaseArea> baseAreas = new ArrayList();
		if(ref.getProvince()!=null) {
			baseAreas =  baseAreaReposity.findByparentid(ref.getProvince());
		}
		model.addAttribute("listcity", baseAreas);
		return "thymeleaf/indexcity";
	}
	
	
	@PostMapping("f/listcounty")
	public String listcounty(Model model,RefDemo ref) {
		List<BaseArea> baseAreas = new ArrayList();
		if(ref.getCity()!=null) {
			baseAreas =  baseAreaReposity.findByparentid(ref.getCity());
		}
		model.addAttribute("listcounty", baseAreas);
		return "freemarker/indexcounty";
	}
	
	@PostMapping("t/list")
	public String list(Model model,RefDemo ref, Page<RefDemo> page) {
		if (null == page) {
			page = new Page<>(1, 2);
		}
		List<RefDemo> refS = refReposity.queryByPage(ref.getAddr(),page.getPageSize()*(page.getpageNo()-1)+1,page.getPageSize());
		/**
		 * h2官方说不支持分页，所以先查询出所有结果，然后进行截取。
		 * 在将结果返回给应用程序之前，数据库会读取所有行。当前不支持服务器端游标。如果只有前几行对应用程序感兴趣，则应限制结果集大小以提高性能。这可以FETCH在查询中使用（例如：SELECT * FROM TEST FETCH FIRST 100 ROWS ONLY）或使用Statement.setMaxRows(max).
		 */
//		refS.subList(0, 0)
		Long count = refReposity.queryByCount(ref.getAddr());
		int start = page.getPageSize()*(page.getpageNo()-1)+1;
		int end = start + page.getPageSize();
		int size = refS.size();
		if(size>0 && size >start) {
			int listend = end<size?end:size;
			refS = refS.subList(start-1, listend-1);
		}
		
		page.setCount(count);
		page.setList(refS);
		model.addAttribute("page", page);
		return "thymeleaf/list";
	}
	
	
	@PostMapping("f/detail")
	public String detail(Model model,RefDemo ref) {
		RefDemo refOne = refReposity.findById(ref.getId()).orElse(new RefDemo());
		model.addAttribute("vo", refOne);
		return "freemarker/detail";
	}
	
	//用于首次加载页面时刷新
	@PostMapping("f/detailrefresh")
	public String detail(Model model) {
		return "freemarker/detail";
	}
	
	
	@GetMapping("j/goadd")
	public String goadd(Model model, HttpServletRequest request) {
		String callBack = request.getParameter("callBack");
		model.addAttribute("callBack", callBack);
		return "jsp/addrefdemo";
	}
	@PostMapping("j/addRefDemo")
	public String addRefDemo(Model model, HttpServletRequest request,RefDemo ref) {
		String callBack = request.getParameter("callBack");
		refReposity.save(ref);
		model.addAttribute("callBack", callBack);
		return "jsp/addrefdemo";
	}
}
